import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

import router.Router;
import server.UDPServer;
import managers.RoutersManager;


/**
 * Classe principal do sistema
 * @author Ana Gabrielle
 * @author Arinaldo Segundo
 * @author Romeryto Lira
 *
 */
public class Main {
	public static void main(String[] args) {
		final String test = args[0];
		final RoutersManager routersManager = new RoutersManager();
		if(!routersManager.contains(args[0])){
			System.err.println("Nao existe roteador com id "+args[0]+" no arquivo de configuracao");
			System.exit(0);
		}	
		final Router router = routersManager.getRouterById(test);
		router.setOn(true);
		router.setNeighbors(routersManager.getNeighbors(test));
		router.setRouterManager(routersManager);
		
		UDPServer udpServer = new UDPServer(router.getPort());
		udpServer.setRouter(router);
		udpServer.start();

		int delay = 10000; //milliseconds
		ActionListener taskPerformer = new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				//...Perform a task...
//				System.out.println("Oi");
				System.out.println(router.toString());
				router.pingNeighbors(routersManager.getNeighbors(test));
			}
		};
		new Timer(delay, taskPerformer).start();
	}
}
